package com.hw.paltfrom.service.system;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseEnterpriseMapper;
import com.hw.paltfrom.pojo.HwBaseEnterprise;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.LanguageUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseEnterpriseVo;


/**
 *源头企业
 * 李俊君
 * 2019年11月6日15:06:04
 */
@Service
public class BaseEnterpriseServer {

	@Autowired
	private HwBaseEnterpriseMapper hwBaseEnterpriseMapper;

	// 源头企业添加
	public Map<String, Object> insert(BaseEnterpriseVo vo) {
			// 先判断源头企业名称 或 编码是否重复
		Wrapper<HwBaseEnterprise> namewrapper = new EntityWrapper<HwBaseEnterprise>();
		namewrapper.eq("F_EnterpriseName", vo.getfEnterprisename()).eq("F_DeleteMark", "0");
		List<HwBaseEnterprise> selectList = hwBaseEnterpriseMapper.selectList(namewrapper);

		if (!selectList.isEmpty()) {
			return ResponeResultUtil.fail(HWSystemCode.ENTERPRISENAME_EXIT);
		}
		
		// 生成ID、创建时间、插入数据库
		HwBaseEnterprise hwBaseEnterprise = new HwBaseEnterprise();
		hwBaseEnterprise.setfEnterpriseid(RandomNumberUtil.getNumber());
		hwBaseEnterprise.setfEnterprisecode(vo.getfEnterprisecode());
		hwBaseEnterprise.setfAreaid(vo.getfAreaid());
		hwBaseEnterprise.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfEnterprisename()));
		hwBaseEnterprise.setfSimplespelling(vo.getfSimplespelling());
		hwBaseEnterprise.setfEnterprisename(vo.getfEnterprisename());
		hwBaseEnterprise.setfLongitude(vo.getfLongitude());
		hwBaseEnterprise.setfLatitude(vo.getfLatitude());
		hwBaseEnterprise.setfAddress(vo.getfAddress());
		hwBaseEnterprise.setfManager(vo.getfManager());
		hwBaseEnterprise.setfTel(vo.getfTel());
		hwBaseEnterprise.setfPic(vo.getfPic());
		hwBaseEnterprise.setfSortcode(vo.getfSortcode());
		hwBaseEnterprise.setfDeletemark(0);
		hwBaseEnterprise.setfEnabledmark(1);
		hwBaseEnterprise.setfDescription(vo.getfDescription());
		hwBaseEnterprise.setfCreatedate(new Timestamp(System.currentTimeMillis()));
		hwBaseEnterprise.setfCreateuserid(vo.getfCreateuserid());
		hwBaseEnterprise.setfCreateusername(vo.getfCreateusername());
		hwBaseEnterprise.setfVideourl(vo.getfVideourl());
		hwBaseEnterprise.setfVideouser(vo.getfVideouser());
		hwBaseEnterprise.setfVideopassword(vo.getfVideopassword());
		hwBaseEnterprise.setfVediochannel(vo.getfVediochannel());
		hwBaseEnterprise.setfVediochannelname(vo.getfVediochannelname());

		Integer insert = hwBaseEnterpriseMapper.insert(hwBaseEnterprise);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	// 源头企业查询
	public Map<String, Object> queryAllList(BaseEnterpriseVo vo) {

		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		PageHelper.startPage(pageNum, pageSize);
		// 模糊查询规则
		Wrapper<HwBaseEnterprise> namewrapper = new EntityWrapper<HwBaseEnterprise>();

		if (StringUtils.isNotBlank(vo.getfAreaid())) {
			namewrapper.eq("F_AreaId", vo.getfAreaid());
		}

		if (StringUtils.isNotBlank(vo.getfEnterprisecode())) {
			namewrapper.like("F_EnterpriseCode", vo.getfEnterprisecode());
		}

		if (StringUtils.isNotBlank(vo.getfEnterprisename())) {
			namewrapper.like("F_EnterpriseName", vo.getfEnterprisename());
		}
		namewrapper.eq("F_DeleteMark",0).orderBy("F_SortCode");
		

		

		List<HwBaseEnterprise> hwBaseEnterprisesList = hwBaseEnterpriseMapper.selectList(namewrapper);
		PageInfo<HwBaseEnterprise> info = new PageInfo<HwBaseEnterprise>(hwBaseEnterprisesList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}

	// 源头企业更新
	public Map<String, Object> update(BaseEnterpriseVo vo) {
		// 通过ID查询待更新的数据
		Wrapper<HwBaseEnterprise> wrapper = new EntityWrapper<HwBaseEnterprise>();
		wrapper.eq("F_EnterpriseID", vo.getfEnterpriseid());

		HwBaseEnterprise hwBaseEnterprise = new HwBaseEnterprise();
		// 如果删除标记为1的话，当前是删除操作
		if (vo.getfDeletemark() == 1) {
			hwBaseEnterprise.setfDeletemark(vo.getfDeletemark());
			hwBaseEnterprise.setfModifydate(new Timestamp(System.currentTimeMillis()));
			hwBaseEnterprise.setfModifyusername(vo.getfModifyusername());
			hwBaseEnterprise.setfModifyuserid(vo.getfModifyuserid());
			Integer update = hwBaseEnterpriseMapper.update(hwBaseEnterprise, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		} else {// 更新数据库

			// 更新自己
			Wrapper<HwBaseEnterprise> namewrapper = new EntityWrapper<HwBaseEnterprise>();
			namewrapper.eq("F_EnterpriseName", vo.getfEnterprisename()).and().notIn("F_EnterpriseID", vo.getfEnterpriseid());
			List<HwBaseEnterprise> hwBaseEnterprisesList = hwBaseEnterpriseMapper.selectList(namewrapper);
			if (!hwBaseEnterprisesList.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.ENTERPRISENAME_EXIT);
			}

			hwBaseEnterprise.setfEnterprisename(vo.getfEnterprisename());
			hwBaseEnterprise.setfEnterprisecode(vo.getfEnterprisecode());
			hwBaseEnterprise.setfSortcode(vo.getfSortcode());
			hwBaseEnterprise.setfManager(vo.getfManager());
			hwBaseEnterprise.setfTel(vo.getfTel());
			hwBaseEnterprise.setfPic(vo.getfPic());
			hwBaseEnterprise.setfAreaid(vo.getfAreaid());
			hwBaseEnterprise.setfLongitude(vo.getfLongitude());
			hwBaseEnterprise.setfLatitude(vo.getfLatitude());
			hwBaseEnterprise.setfAddress(vo.getfAddress());
			hwBaseEnterprise.setfDeletemark(vo.getfDeletemark());
			hwBaseEnterprise.setfEnabledmark(vo.getfEnabledmark());
			hwBaseEnterprise.setfDescription(vo.getfDescription());
			hwBaseEnterprise.setfModifydate(new Timestamp(System.currentTimeMillis()));
			hwBaseEnterprise.setfModifyuserid(vo.getfModifyuserid());
			hwBaseEnterprise.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfEnterprisename()));
			hwBaseEnterprise.setfSimplespelling(vo.getfSimplespelling());
			hwBaseEnterprise.setfModifyusername(vo.getfModifyusername());
			
			hwBaseEnterprise.setfVideourl(vo.getfVideourl());
			hwBaseEnterprise.setfVideouser(vo.getfVideouser());
			hwBaseEnterprise.setfVideopassword(vo.getfVideopassword());
			hwBaseEnterprise.setfVediochannel(vo.getfVediochannel());
			hwBaseEnterprise.setfVediochannelname(vo.getfVediochannelname());
			Integer update = hwBaseEnterpriseMapper.update(hwBaseEnterprise, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}
}
